const AWS = require('aws-sdk');
AWS.config.update({region:'us-east-2'});
const apigateway = new AWS.APIGateway();

const d = {};

d.getRestApis = (params) => new Promise((resolve, reject) => {
  apigateway.getRestApis(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.getVpcLinks = (params) => new Promise((resolve, reject) => {
  apigateway.getVpcLinks(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

module.exports = d;

if (require.main === module) {
  (async() => {
    // const result = await d.getResources({restApiId: "a4xk2e6ke0"});
    // console.log(result);
    const getRestApisResp = await d.getRestApis({limit: "100"});
    if (getRestApisResp.items.length >= 100) {
      console.warn("exceeded the limit of getRestApis, the results might miss some items.")
    }
    const filteredApiGateways = getRestApisResp.items.filter(item => {
      return item.name === "pt-private-gateway";
    });
    if (filteredApiGateways.length > 0){
      return {
        Id: filteredApiGateways[0].id,
        Name: filteredApiGateways[0].name,
        Types: filteredApiGateways[0].endpointConfiguration.types
      }
    } else {
      return null;
    }

    console.log(getRestApisResp);
  })();
}